/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import core.IMAPMIDlet;
import core.ScreenEngine;
import core.Settings;
import view.additional.CellItem;
import view.additional.MenuList;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class ThemeEditForm extends BasicForm implements MenuList.MenuListSelection
{
    private static final int SET_COMMAND_ID = 1;
    
    private static final int DAY_THEME_ITEM_ID = 0;
    private static final int NIGHT_THEME_ITEM_ID = 1;
    
    private MenuList items;
    
    public ThemeEditForm()
    {
        super("themes");
        
        init();
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setScrollable(false);
        
        items = new MenuList();
        items.addItem(new CellItem(DAY_THEME_ITEM_ID, "day_theme"));
        items.addItem(new CellItem(NIGHT_THEME_ITEM_ID, "night_theme"));
        items.setSelectionListener(this);
        addComponent(items);
                
        //Команды
        addCommand(new BackCommand()); 
        
        Command set = new Command("set", SET_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                itemSelected(items.getSelectedId());
                
                evt.consume();
            }
        };
        addCommand(set);
    }
    
    public void show()
    {
        items.setSelectedIndex(0);
        
        super.show();
    }
    
    public void itemSelected(int id)
    {
        Settings settings = Settings.getSettings();
        String oldTheme = settings.getTheme();
        String newTheme = "";
    
        if (id == DAY_THEME_ITEM_ID)
        {
            newTheme = Settings.THEME_DAY;
        }
        else if (id == NIGHT_THEME_ITEM_ID)
        {
            newTheme = Settings.THEME_NIGHT;
        }
        if (!oldTheme.equals(newTheme))
        {
            settings.setTheme(newTheme);
            settings.save();
        }
        
        //Обновляем тему
        refreshTheme = true;
        IMAPMIDlet.getMidlet().setTheme();
        
        //Возвращаемся на главную
        ScreenEngine.getEngine().popTo(0);
    }
}
